package com.csc.training.DAO;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.csc.training.bean.Student;
import com.csc.training.inf.StudentDAO;
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class StudentDAOImpl implements StudentDAO {
	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;

	@Override
	public void addStudent(Student student) {
		session.save(student);

	}

	@Override
	public void updateStudent(Student student) {
		session.update(student);

	}

	@Override
	public void deleteStudent(Student student) {
		session.delete(student);

	}

	@Override
	public List<Student> listStudent() {
		return session.createQuery("from Student").list();
	}

	public List<Student> findStudentByName(String name) {
		return session
				.createQuery(
						"from Student s where s.studentName like :studentName")
				.setParameter("studentName", '%' + name + '%').list();
	}
	public Student getStudentById(long studentId) {
		return (Student)session
				.createQuery("From Student s where s.studentId= :studentId")
				.setParameter("studentId", studentId).list().get(0);
	}
}
